package com.example.locationtracking.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.locationtracking.R;
import com.example.locationtracking.model.POSTResponse;
import com.example.locationtracking.model.User;
import com.example.locationtracking.service.Service;
import com.example.locationtracking.service.impl.ServiceFactory;
import com.example.locationtracking.utils.Const;
import com.example.locationtracking.utils.LTUtils;

public class RegisterActivity extends BaseActivity {

	private EditText userName;
	private EditText password;
	private EditText pinCode;
	private Button login;
	private Button cancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(!LTUtils.checkInternetConnection(this)){
        	Intent intent = new Intent(this,NetWorkErrorActivity.class);
    		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    		startActivityForResult(intent, Const.NETWORK_ACTIVITY_REQUEST_CODE);
    		finish();
			return;
		}
		setContentView(R.layout.register_activity);
		initUI();
		setControls();
	}
	
	protected void initUI(){
		userName = (EditText) findViewById(R.id.txt_user_name);
		password = (EditText) findViewById(R.id.txt_password);
		pinCode = (EditText) findViewById(R.id.txt_pin_code);
		login = (Button) findViewById(R.id.bt_login);
		cancel = (Button) findViewById(R.id.bt_cancel);
	}

	private void setControls() {
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				userName.setText("");
				password.setText("");
				pinCode.setText("");
				finish();
			}
		});
		login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						Message msg = handler.obtainMessage();
						
						if (userName.getText().toString().equals("")) {
							return;
						} else if (password.getText().toString().equals("")) {
							return;
						} else if (pinCode.getText().toString().equals("")) {
							return;
						} else {
							User user = new User();
							user.setUserName(userName.getText().toString());
							user.setPassword(password.getText().toString());
							user.setPin(pinCode.getText().toString());

							Service service = ServiceFactory.getInstance()
									.getService();
							try {

								POSTResponse postResponse = service
										.register(user);
								
				
								msg.obj = postResponse;
								
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								
							}
							
						}
							handler.sendMessage(msg);
					}
				});
				showProgressDlg();
				thread.start();
			}
		});
	}
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if (msg.obj != null) {
				POSTResponse isRegister = (POSTResponse) msg.obj;
				if (isRegister.getResult()) {
					LTUtils.saveValue(getApplicationContext(), Const.USER_NAME_KEY, userName.getText().toString());
					Intent intent = new Intent(RegisterActivity.this,
							MapActivity.class);
					intent.putExtra(Const.USER_NAME_KEY, userName.getText().toString());
					startActivity(intent);
					RegisterActivity.this.finish();
				}else{
					Toast.makeText(RegisterActivity.this, isRegister.getMsg(), Toast.LENGTH_LONG).show();
				}
			//Log.e("register response",msg.obj+"");
			} else {
				showNetworkErrorDlg();
			}
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	   return false;
	}
}
